<?php

namespace App\Http\Controllers;

use App\Entities\ClassMember;
use App\Repositories\ClassMemberRepository;
use App\Repositories\ClassRepository;
use Illuminate\Http\Request;

class ClassMemberController extends Controller
{
    private $classMember;
    private $classEntity;

    public function __construct (
        ClassMemberRepository $classMember,
        ClassRepository $classEntity
    )
    {
        $this->classMember = $classMember;
        $this->classEntity = $classEntity;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $classMember = $this->classMember->findByAttributes([
            'member_id' => $request->get('member_id'),
            'class_id' => $request->get('class_id')
        ]);
        $classEntity = $this->classEntity->findWith($request->get('class_id'),['members']);
        if(!$classEntity) {
            return response()->json([
                'errors' => true,
                'message' => '班级不存在',
            ]);
        }else {
//            dd($classEntity->members->count());
            if($classEntity->members->count() >= $classEntity->member_limit) {
                return response()->json([
                    'errors' => true,
                    'message' => '超出人数限制',
                ]);
            }
        }
        if(!$classMember){
            $classMember = $this->classMember->create($request->all());
            return response()->json([
                'errors' => false,
                'message' => '新建成功',
                'data' => $classMember
            ]);
        }else {
            return response()->json([
                'errors' => true,
                'message' => '该班学员已存在',
                'data' => $classMember
            ]);
        }


    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Entities\ClassMember  $classMember
     * @return \Illuminate\Http\Response
     */
    public function show(ClassMember $classMember)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Entities\ClassMember  $classMember
     * @return \Illuminate\Http\Response
     */
    public function edit(ClassMember $classMember)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Entities\ClassMember  $classMember
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, ClassMember $classMember)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Entities\ClassMember  $classMember
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        if(strpos($id,'_')) {
            $ids = explode('_', $id);
            $class_id = $ids[0];
            $member_id = $id[1];

            $classMember = $this->classMember->findByAttributes([
                'class_id' => $class_id,
                'member_id' => $member_id
            ]);

        }else {
            $classMember = $this->classMember->find($id);
        }


        if(!$classMember) {
            return response()->json([
                'errors' =>  true,
                'msg' => '没有该数据！'
            ]);
        }

        $ret  = $this->classMember->destroy($classMember);

        return response()->json([
            'errors' =>  false,
            'data' => $ret
        ]);

    }
}
